package com.qf.controller;

import com.qf.constats.Status;
import com.qf.model.Device;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.page.Result;
import com.qf.service.DeviceService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("device")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;
    @RequestMapping("findAll")
    public List<Device> findAll(){
       List<Device> devices= deviceService.findAll();
       return devices;
    }
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult result = deviceService.findPage(queryPageBean);
        return  result;
    }

    @RequestMapping("add")
    public Result add(@RequestBody Device device){
        int row = deviceService.addDevice(device);
        if (row>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }
    @GetMapping("findById")
    public Result findById(@RequestParam("id") int id){
        Device device = deviceService.findById(id);
        Result result = new Result();
        result.setMessage(Status.FAIL.getMessage());
        if (ObjectUtils.isNotEmpty(device)){
            result.setFlag(true);
            result.setData(device);
            result.setMessage(Status.SUCCESS.getMessage());
        }
        return result;

    }

    @PostMapping("edit")
    public Result edit(@RequestBody Device device){
        int row  =  deviceService.edit(device);
        if (row>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }
    @GetMapping("delete")
    public Result delete(@RequestParam("id") Integer id){
        int row =  deviceService.deleteById(id);
        if (row>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }


}
